package simple.utils.data.serializers;

import simple.utils.tracking.TrackingUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * Created by martin on 7.5.14.
 */
public class EnumSerializer implements Serializer<Enum> {

    private static final String SEPARATOR = ":::";

    @Override
    public byte[] toBytes(Enum anEnum) throws IOException {
        String s = anEnum.getClass().getName() + SEPARATOR + anEnum.name();
        return s.getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public Enum fromBytes(byte[] data) throws IOException {
        String s = new String(data, StandardCharsets.UTF_8);
        String[] split = s.split(SEPARATOR);
        System.out.println(Arrays.toString(split));
        Class cl= null;
        try {
            cl = Class.forName(split[0]);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String en = split[1];
        return Enum.valueOf(cl, en);
    }
}
